﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Meeting.Public;
using System.Collections;
using System.Data.SqlClient;
using System.Data;
using Meeting.DBUtility;
using Newtonsoft.Json;

namespace Meeting.BLL
{
    /// <summary>
    /// 模块功能：保存常设会议相关信息
    /// 模块日期：20130119
    /// 模块作者：wlb
    /// </summary>
    public class StandMeeting
    {
        public string errorString = "";

        //获取会议类型
        public string GetMeetingCategory(string Id)
        {
            string sql = "select Convert(nvarchar,Id )+ '#' + RoleId As Id,Name from  MeetingCategory ";
            if (Id != "")
            {
                sql += " Where Id = " + Id;
            }
            try
            {
                DataSet ds = DbHelperSQL.Query(sql);
                if (ds == null) return "";
                return JsonConvert.SerializeObject(ds.Tables[0]);
            }
            catch { return ""; }
        }


        #region 保存常设会议信息
        public bool SaveStandMeeting(Model.StandMeeting model, string selEmployee)
        {

            try
            {

                List<DictionaryEntry> dicList = new List<DictionaryEntry>();

                //主表
                if (model.Id == Guid.Empty)
                {
                    model.Id = Guid.NewGuid();
                    dicList.Add(PreAdd(model));
                }
                else
                {
                    dicList.Add(PreUpdate(model));
                    dicList.Add(PreDeleteMeetingEmployee(model.Id));
                }
                List<Model.StandMeetingEmployee> listEmp = JsonConvert.DeserializeObject<List<Model.StandMeetingEmployee>>(selEmployee);
                //子表
                foreach (Model.StandMeetingEmployee myEmp in listEmp)
                {
                    Model.StandMeetingEmployee emp = new Model.StandMeetingEmployee();
                    emp.Id = Guid.NewGuid();
                    emp.MeetingId = model.Id;
                    emp.EmployeeId = myEmp.EmployeeId;
                    emp.EmployeeName = myEmp.EmployeeName;
                    emp.EmployeeTitle = myEmp.EmployeeTitle;
                    emp.Sort = CommonFunction.varToIntDef(myEmp.Sort, 0);
                    emp.RoleId = myEmp.RoleId;
                    emp.Mobile = myEmp.Mobile;
                    dicList.Add(PreAddMeetingEmployee(emp));
                }
                DBUtility.DbHelperSQL.ExecuteSqlTran(dicList);
                errorString = model.Id.ToString();
                return true;
            }
            catch (Exception ex)
            {
                errorString = ex.Message;
                return false;
            }
        }

        /// <summary>
        /// 增加一条数据 常设会议主表
        /// </summary>
        public DictionaryEntry PreAdd(Model.StandMeeting model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into StandMeeting(");
            strSql.Append("Id,CategoryId,Name,Comment,IsActive)");
            strSql.Append(" values (");
            strSql.Append("@Id,@CategoryId,@Name,@Comment,@IsActive)");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.UniqueIdentifier,16),
					new SqlParameter("@CategoryId", SqlDbType.Int,4),
					new SqlParameter("@Name", SqlDbType.NVarChar,128),
					new SqlParameter("@Comment", SqlDbType.NVarChar,200),
					new SqlParameter("@IsActive", SqlDbType.Bit,1)};
            parameters[0].Value = model.Id;
            parameters[1].Value = model.CategoryId;
            parameters[2].Value = model.Name;
            parameters[3].Value = model.Comment;
            parameters[4].Value = model.IsActive;

            DictionaryEntry myDE = new DictionaryEntry();
            myDE.Key = strSql;
            myDE.Value = parameters;
            return myDE;
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public DictionaryEntry PreUpdate(Model.StandMeeting model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update StandMeeting set ");
            strSql.Append("CategoryId=@CategoryId,");
            strSql.Append("Name=@Name,");
            strSql.Append("Comment=@Comment,");
            strSql.Append("IsActive=@IsActive");
            strSql.Append(" where Id=@Id ");
            SqlParameter[] parameters = {
					new SqlParameter("@CategoryId", SqlDbType.Int,4),
					new SqlParameter("@Name", SqlDbType.NVarChar,128),
					new SqlParameter("@Comment", SqlDbType.NVarChar,200),
					new SqlParameter("@IsActive", SqlDbType.Bit,1),
					new SqlParameter("@Id", SqlDbType.UniqueIdentifier,16)};
            parameters[0].Value = model.CategoryId;
            parameters[1].Value = model.Name;
            parameters[2].Value = model.Comment;
            parameters[3].Value = model.IsActive;
            parameters[4].Value = model.Id;

            DictionaryEntry myDE = new DictionaryEntry();
            myDE.Key = strSql;
            myDE.Value = parameters;
            return myDE;
        }

        /// <summary>
        /// 删除子表数据
        /// </summary>
        public DictionaryEntry PreDeleteMeetingEmployee(Guid Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from StandMeetingEmployee ");
            strSql.Append(" where MeetingId=@MeetingId ");
            SqlParameter[] parameters = {
					new SqlParameter("@MeetingId", SqlDbType.UniqueIdentifier,16)};
            parameters[0].Value = Id;
            DictionaryEntry myDE = new DictionaryEntry();
            myDE.Key = strSql;
            myDE.Value = parameters;
            return myDE;
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public DictionaryEntry PreAddMeetingEmployee(Model.StandMeetingEmployee model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into StandMeetingEmployee(");
            strSql.Append("Id,MeetingId,EmployeeId,EmployeeName,RoleId,EmployeeTitle,Sort,Mobile)");
            strSql.Append(" values (");
            strSql.Append("@Id,@MeetingId,@EmployeeId,@EmployeeName,@RoleId,@EmployeeTitle,@Sort,@Mobile)");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.UniqueIdentifier,16),
					new SqlParameter("@MeetingId", SqlDbType.UniqueIdentifier,16),
					new SqlParameter("@EmployeeId", SqlDbType.NVarChar,64),
					new SqlParameter("@EmployeeName", SqlDbType.NVarChar,64),
					new SqlParameter("@RoleId", SqlDbType.NVarChar,1024),
					new SqlParameter("@EmployeeTitle", SqlDbType.NVarChar,64),
					new SqlParameter("@Sort", SqlDbType.Int,4),
					new SqlParameter("@Mobile", SqlDbType.NVarChar,64)};
            parameters[0].Value = model.Id;
            parameters[1].Value = model.MeetingId;
            parameters[2].Value = model.EmployeeId;
            parameters[3].Value = model.EmployeeName;
            parameters[4].Value = model.RoleId;
            parameters[5].Value = model.EmployeeTitle;
            parameters[6].Value = model.Sort;
            parameters[7].Value = model.Mobile;

            DictionaryEntry myDE = new DictionaryEntry();
            myDE.Key = strSql;
            myDE.Value = parameters;
            return myDE;
        }

        //删除会议信息
        public bool DeleteStandMeeting(string Id)
        {
            string sqlStr = "  Delete From StandMeetingEmployee Where MeetingId = '" + Id + "'; ";
            sqlStr += " Delete From StandMeeting Where Id = '" + Id + "'  ";
            try
            {
                if (DbHelperSQL.ExecuteSql(sqlStr) > 0) return true;
                return false;
            }
            catch (Exception ex)
            {
                errorString = ex.Message;
                return false;
            }
        }
        #endregion

        #region 查询常设会议数据
        //我需要参加的会议
        public string GetStandMeeting(int pageIndex, int pageSize, string filter)
        {
            try
            {
                int nAllRecord = 0, nAllPages = 0;
                DataTable dt = DbHelperSQL.GetPage(" cast(Id as varchar(50))  ", " * ", "StandMeeting", filter, " Id ",
                                pageIndex, pageSize, out nAllRecord, out nAllPages);
                if (dt == null) return "";
                Model.JsonGrid jr = new Model.JsonGrid();
                jr.total = nAllRecord;
                jr.rows = dt;

                //Newtonsoft.Json.Converters.IsoDateTimeConverter timeConverter = new Newtonsoft.Json.Converters.IsoDateTimeConverter();
                //timeConverter.DateTimeFormat = "yyyy'-'MM'-'dd' 'HH':'mm";

                return JsonConvert.SerializeObject(jr);
            }
            catch (Exception ex)
            {
                errorString = ex.Message;
                return "";
            }
        }
        //获取主会议信息
        public string GetMeetingById(string id,string CategoryId)
        {
            string sql = "Select * From StandMeeting Where 1=1 ";
            if (id != "") sql += " And Id = '" + id + "' ";
            if (CategoryId != "") sql += " And CategoryId = " + CategoryId;
            DataSet ds = DbHelperSQL.Query(sql);
            if (ds == null) return "";
            return JsonConvert.SerializeObject(ds.Tables[0]);
        }
        //获取已选人员信息
        public string GetMeetingEmployeeById(string id)
        {
           /* DataSet ds = DbHelperSQL.Query("Select distinct(RoleId) From StandMeetingEmployee Where MeetingId = '" + id + "' ");
            List<OaUser> userList = new List<OaUser>();
            if (ds == null || ds.Tables[0].Rows.Count < 1) return "";
            Employee bll = new Employee();
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                OaUser ou = new OaUser();
                ou.account = dr["RoleId"].ToString();
                ou.name = bll.GetName(ou.name);
                ou.parent = "0";
                ou.nodetype = "role";
                userList.Add(ou);
            }
            ds = DbHelperSQL.Query("Select * From StandMeetingEmployee Where MeetingId = '" + id + "' ");
            if (ds == null) return "";
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                OaUser ou = new OaUser();
                ou.account = dr["EmployeeId"].ToString();
                ou.name = dr["EmployeeName"].ToString();
                ou.parent = dr["RoleId"].ToString();
                ou.nodetype = "user";
                userList.Add(ou);
            }*/
            DataSet ds = DbHelperSQL.Query("Select * From StandMeetingEmployee Where MeetingId = '" + id + "' Order by Sort ");
            if (ds == null || ds.Tables[0].Rows.Count < 1) return "";
            return JsonConvert.SerializeObject(ds.Tables[0]);
        }
        #endregion
    }
}
